﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using Emoney.Spider.Basic;

public partial class gold_eGold_CodeCreate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
            CMSModel.eGold_Code ec = new CMSModel.eGold_Code();

            var news = (from c in dal.eGold_Code
                        orderby c.date descending
                        select c
                        ).Take(1);
            try {
                ec = news.First();
                Label1.Text = "密码生成到：" + ec.date.ToString();
            }
            catch(Exception ex) {
                Label1.Text = "目前尚未生成过密码。" ;
            }
          
           
     

           
   
        }

    }


    protected void Button1_Click(object sender, EventArgs e)
    {

        char[] Pattern = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
        int n = Pattern.Length;
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        CMSModel.eGold_Code  ec = null;
        DateTime dt = System.DateTime.Now.Date;
        string str = "";

        //获得最新一期
        var news = (from c in dal.eGold_Code
                    orderby c.date descending
                    select c
                       ).Take(1);
        try
        {
            CMSModel.eGold_Code ec1 = news.First();
            dt = ec1.date.AddDays(1);
        }
        catch (Exception ex)
        { 
        
        }
       
        //--------------------------------------------
        for (int i = 0; i < 100; i++)
        {

            System.Random rd = new Random();
            for (int j = 0; j < 8; j++)
            {
                int ir = rd.Next(0, n);
                str += Pattern[ir];
            }
            str += System.DateTime.Now.Date.ToString();
            string strPwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
            strPwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strPwd, "MD5"); ;
          
            ec = new CMSModel.eGold_Code();
            ec.code = strPwd;
            ec.date = dt.AddDays(i);

            dal.AddToeGold_Code(ec);
           
        }

        dal.SaveChanges();       

    }


}